<?php
/**
 * Created by PhpStorm.
 * @author LiShaoWei
 * @param Date : 2016/4/11
 * @param Time : 14:26
 * @since 1.0
 */
namespace app\controllers;
use app\models\BookArticle;
use app\models\Books;
use app\models\Corp;
use app\models\CorpLog;
use app\models\User;
use Yii;
use yii\base\Exception;
use yii\rest\ActiveController;
use yii\web\Controller;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;
use yii\web\Response;

use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBasicAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\auth\QueryParamAuth;

class BoodsController extends ActiveController
{
    public $modelClass = 'app\models\Images';
    public function actionWhile()
    {
        $booksModel=new Books();
        while(true){
            $model=$booksModel::find()->where(['state'=>'YES'])->one();
            $this->Fun($model['table_id'],$model['socure']);
            $model->state='GET';
            $model->save();
        }
    }
    /**
     * 抓目录
     */
    public function Fun($id,$url)
    {
//        http://www.nanrenshu.com/Novel.Asp?ID=368&Y=-1
        $BookArticleModel= new Books();
//        $id='147307080618566';
//        $url="http://www.biquge.info/23_23468/";
        $strIng=$this->vget($url,'');
        echo $strIng;
//        $tt = '/\[{".*?"}\]/si';
        $strZheng='/<dd>.*?<\/dd>/si';
        preg_match_all ( $strZheng , $strIng , $array );//匹配出地址和内容
        $imageurl=$this->strSubstr1('http','jpg',$this->strSubstr1('src="http','jpg"',$strIng));
        $array1=array(
            'cont'=>file_get_contents($imageurl),
            'cont_type'=>'image/png',
            'path'=>'book'
        );
        $image= $this->curl_post('http://y.com/images/create',$array1);
        $BooksModel=new Books();
        $explain=$this->strSubstr('id="intro">','<div id="sidebar',$strIng);
        $explain=$this->strSubstr('<p>','</p>',$explain);
        $BooksModel->find()->createCommand()->update(
            'books',[
                'images'=>strtr($image,['"'=>'']),
                'explain'=>iconv("gbk","UTF-8", $explain) ,
            ],['table_id'=>$id]
        )->execute();
//        $i=0;
//        foreach($array[0] as $s=>$k){
//            IF(empty($k)){
//
//            }else{
//                preg_match('/[0-9]{4,}\w*\.html/si',$k,$arrayUrl); //撇配地址
//                if(empty($arrayUrl[0])){
//                    //return '成功解析'. $i.'条';
//                     return $i;
//                }
//                $rt=json_encode($arrayUrl,true);
//                $urlTitle=$url.$arrayUrl[0];
//                $table='book_'.$id;
//
//                $sql="select id from $table where socure='$urlTitle'";
//                if(!Yii::$app->db->createCommand($sql)->queryOne()){
//                    $BookArticleModel::find()->createCommand()->insert('log',
//                        [
//                            'log'=>iconv("gbk","UTF-8", $rt) ,
//                            'log2'=>iconv("gbk","UTF-8", $arrayUrl[0]),
//                            'book_id'=>$table,
//                            'url'=>$urlTitle,
//                            'is_state'=>0,
//                            'create_time'=>time()
//                        ])->execute();
//                    $this->Set($urlTitle,$id);
//                    $i++;
//                }

        // echo '成功解析'. $i.'条';
       // return $i;
    }
    public function strSubstr($beginStr, $endStr, $contStr) {
        $beginStrNo = strpos($contStr, $beginStr);
        $endStrNo = strpos($contStr, $endStr, $beginStrNo);

        $length = $endStrNo - $beginStrNo - strlen($beginStr);
        $cont = substr($contStr, $beginStrNo + strlen($beginStr), $length);

        return trim($cont);
    }
    public function strSubstr1($beginStr, $endStr, $contStr) {
        $beginStrNo = strpos($contStr, $beginStr);
        $endStrNo = strpos($contStr, $endStr, $beginStrNo);

        $length = $endStrNo - $beginStrNo+strlen($endStr) ;
        $cont = substr($contStr, $beginStrNo , $length);

        return trim($cont);
    }
    /**
     * 抓取内容
     * @author  LiShaoWei
     * @access  public
     * @since   1.0
     */
    public function Set($urlTitle1,$id)
    {
        $BookArticleModel= new BookArticle();
        $strIngTitleContent=$this->vget($urlTitle1,'');
        preg_match('/<h1>.*?<\/h1>/si',$strIngTitleContent,$arrayTitle); //匹配标题
        $title=strtr($arrayTitle[0],array('<h1>'=>'','</h1>'=>''));//去除多余
        preg_match('/id="content".*?<\/div>/si',$strIngTitleContent,$arrayTitleContent); //匹配标题
        $arrayContent=strtr($arrayTitleContent[0],array('&nbsp;'=>'','</div>'=>''));//去除多余
        $pattern = "/<.*>/";
        $pattern1="/id=.*? >/si";
        $content= preg_replace($pattern,"",$arrayContent);
        $content1= preg_replace($pattern1,"",$content);
        $title=iconv("gbk","UTF-8", $title);
        $i=0;
        $table='book_'.$id;
        $sql="select id from $table where title='$title'";
        if(empty($content1))
        {
            $content1='';
        }else{
            try{
                $content1=iconv( "gbk","UTF-8", htmlspecialchars($content1));
            }catch(Exception $e){
                var_dump($content1);die();
            }
        }
        if(Yii::$app->db->createCommand($sql)->queryOne()){

        }else{
            $BookArticleModel::find()->createCommand()->insert($table,[
                'title'=>$title,
                'content'=> $content1,
                'socure'=>$urlTitle1,
                'create_time'=>time()
            ] )->execute();
            $i++;
        }

        return $i;
    }
    public function actionZz()
    {
        for($i=1;$i<=913;$i++){
            $URL='http://www.biquge.info/list/_'.$i.'.html';
            $this->Zz($URL);
            echo $URL.'/n';
            sleep(3);
        }
    }
    /**
     * 抓取分类所有
     * @author  LiShaoWei
     * @access  public
     * @since   1.0
     * @throws \yii\db\Exception
     */
    public function Zz($URL)
    {
        //$URL='http://www.biquge.info/list/1_3.html';
        $str=$this->vget($URL,'');
        // return $str;
        //$strZheng='/cpos="title".*?class="/si';
        $strZheng ='/<li><span class=\"s1\">.*?<\/li>/si';
        preg_match_all ( $strZheng , $str , $array );//匹配出地址和内容
        $BOOK= new Books();
        foreach($array[0] as $ks=>$kv){
            if(strlen($kv)>290){
                $strZheng1='/http:\/\/.*?\/"/si';
                $strZheng2='/\/">.*?<\/a>/si';
                $strZheng3='/">.*?]/si';
                $strZheng4='/"s4">.*?<\/span>/si';
                preg_match ( $strZheng2 , $kv , $array2 );//书名
                $array2=strtr($array2[0],['/">'=>'',"</a>"=>'']);
                $title=iconv("gbk","UTF-8", $array2);
                if($BOOK::find()->where(['name'=>$title])->one()){

                }else{
                    preg_match ( $strZheng1 , $kv , $array1 );//匹配出地址和内容
                    preg_match ( $strZheng3 , $kv , $array3 );//类别
                    preg_match ( $strZheng4 , $kv , $array4 );//作者
                    $array3=strtr($array3[0],['">['=>'',"]"=>'']);
                    $array4=strtr($array4[0],['"s4">'=>'',"</span>"=>'']);
                    $bookArray=array(
                        'uid'=>$this->generateuuid(),
                        'name'=>iconv("gbk","UTF-8", $array2),
                        'table_id'=>$this->getTable($title),
                        'author'=>iconv("gbk","UTF-8", $array4),
                        'category'=>iconv("gbk","UTF-8", $array3),
                        'socure'=>substr($array1[0],0,-1),
                        'create_time'=>time(),
                        'update_time'=>time());
                    $BOOK::find()->createCommand()->insert(
                        'books',$bookArray
                    )->execute();
                    $BOOK->safeUp($bookArray['table_id']);
                    echo '-'.$title.'<br>';
                }
            }}
    }


    public function getTable($title)
    {
        return time().substr(md5($title),0,5);
    }
    public function generateuuid()
    {
        // $this->Cai(substr($array1[0],0,-1),$id);
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
        $hyphen = '';
        $hyphen = chr(45);
        $uuid = substr($charid, 0, 8).$hyphen
            .substr($charid, 8, 4).$hyphen
            .substr($charid,12, 4).$hyphen
            .substr($charid,16, 4).$hyphen
            .substr($charid,20,12);
        return $uuid;
    }
    function vget ( $url , $ucookie )
    { // 模拟登录获取Cookie函数

        $curl = curl_init (); // 启动一个CURL会话
        curl_setopt ( $curl , CURLOPT_URL , $url ); // 要访问的地址
        curl_setopt ( $curl , CURLOPT_SSL_VERIFYPEER , 0 ); // 对认证证书来源的检查
        curl_setopt ( $curl , CURLOPT_SSL_VERIFYHOST , 2 ); // 从证书中检查SSL加密算法是否存在
        //curl_setopt ( $curl , CURLOPT_USERAGENT , $_SERVER['HTTP_USER_AGENT'] ); // 模拟用户使用的浏览器
        curl_setopt ( $curl , CURLOPT_FOLLOWLOCATION , 1 ); // 使用自动跳转
        curl_setopt ( $curl , CURLOPT_AUTOREFERER , 1 ); // 自动设置Referer
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt ( $curl , CURLOPT_COOKIEJAR , $ucookie ); // 存放Cookie信息的文件名称
        curl_setopt ( $curl , CURLOPT_COOKIEFILE , $ucookie ); // 读取上面所储存的Cookie信息
        curl_setopt ( $curl , CURLOPT_TIMEOUT , 30 ); // 设置超时限制防止死循环
        curl_setopt ( $curl , CURLOPT_HEADER , 0 ); // 显示返回的Header区域内容
        curl_setopt ( $curl , CURLOPT_RETURNTRANSFER , 1 ); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec ( $curl ); // 执行操作
        if (curl_errno ( $curl )) {
            echo 'Errno' . curl_error ( $curl );
        }
        curl_close ( $curl ); // 关闭CURL会话
        return $tmpInfo; // 返回数据
    }
    public function curl_post($remote_server, $post_arr, $headers = NULL, $cookie_path = NULL, $use_cookie = FALSE, $proxy = NULL, $upload = FALSE, $timeout = NULL, $refer = NULL)
    {
        if (!$upload) {
            $post_data = http_build_query($post_arr);
        } else {
            $post_data = $post_arr;
        }
        $ch = curl_init();
        if (!is_null($proxy)) {
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
            curl_setopt($ch, CURLOPT_PROXY, $proxy);
            curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
        }
        if (is_null($timeout)) {
            $timeout = 30;
        }
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $remote_server);
        if (!is_null($headers)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        if ($use_cookie && !is_null($cookie_path)) {
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
        }
        if (!is_null($cookie_path)) {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
        }
        if (!is_null($refer)) {
            curl_setopt($ch, CURLOPT_REFERER, $refer);
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}